package com.mooc.yohoshop.ui.order;

import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.cooltechworks.views.shimmer.ShimmerRecyclerView;
import com.mooc.yohoshop.App;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.OrderEntity;
import com.mooc.yohoshop.entity.UpdateOrderEntity;
import com.mooc.yohoshop.ui.base.BaseLazyFragment;
import com.mooc.yohoshop.widget.SecKillDivider;
import java.util.HashMap;

public class OrderFragment extends BaseLazyFragment<OrderRepo> implements OnItemChildClickListener {

    private ShimmerRecyclerView order_rv;
    private OrderAdapter adapter;
    private int statues = 0;

    @Override
    protected void lazyInit() {
        statues = getArguments().getInt("statues");
        HashMap<String,Object>map=new HashMap<>();
        map.put("user_id",""+ App.getInstance().getUserId());
        map.put("statues",""+statues);
        request(repo.requestOrderList(map));
    }

    @Override
    protected int layout() {
        return R.layout.fragment_order;
    }

    @Override
    protected void init() {
        order_rv = f(R.id.order_rv);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new OrderAdapter();
        adapter.setOnItemChildClickListener(this);
        order_rv.setLayoutManager(manager);
        order_rv.addItemDecoration(new SecKillDivider(getContext()));
        order_rv.setAdapter(adapter);
        order_rv.showShimmerAdapter();
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof OrderEntity){
            dialog.dismiss();
            OrderEntity orderEntity = (OrderEntity) baseEntity;
            String str = "";
            //0待支付，1待收货，2已完成,3已取消
            if (!orderEntity.data.isEmpty()) {
                if (orderEntity.data.get(0).order_statues == 0) {
                    str = "待支付";
                } else if (orderEntity.data.get(0).order_statues == 1) {
                    str = "待收货";
                } else if (orderEntity.data.get(0).order_statues == 2) {
                    str = "已完成";
                } else {
                    str = "已取消";
                }
                for (int i = 0; i < orderEntity.data.get(0).order_goodsValues.size(); i++)
                    orderEntity.data.get(0).order_goodsValues.get(i).statue = str;
                adapter.setNewInstance(orderEntity.data.get(0).order_goodsValues);
                order_rv.hideShimmerAdapter();

            }
        }else if (baseEntity instanceof UpdateOrderEntity){
            if (baseEntity.message.equals("修改成功"))
                lazyInit();
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        OrderEntity.OrderGoodsValues entity = this.adapter.getItem(position);
        entity.statue = ""+2;
        request(repo.requestUpdateOrder(entity));
    }
}
